import {Form,Input,Button,message} from 'antd'
import {useNavigate} from 'react-router-dom'
import IUser from '@/types/IUser'
import api from '@/api'
export default ()=>{
    const nav=useNavigate()
    const login=async(values:IUser)=>{
       const result=await api.users.login(values)
       if(result.data.code){
         message.success(result.data.message)
         //保存token到localStorage
         localStorage.setItem('token',result.data.token)
         //跳转
         nav('/')
        }else{
            message.error(result.data.message)
        }
    
    }
    return (
        <Form
          onFinish={login}>
            <Form.Item
                label="用户名" 
                name="username">
                    <Input placeholder='请输入用户名'/>
            </Form.Item>
            <Form.Item
                label="密码"
                name="password">
                <Input.Password placeholder='请输入密码'/>
            </Form.Item>
            <Form.Item>
                <Button type='primary' htmlType='submit'>登录</Button>
            </Form.Item>
        </Form>
    )
}